﻿package mortal.game.view.task.drama.operations
{
    import Message.DB.Tables.*;
    import flash.geom.*;
    import mortal.game.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.game.scene3D.ai.*;
    import mortal.game.scene3D.player.entity.*;
    import mortal.mvc.core.*;

    public class TaskDramaPlayerMoveOnly extends Object implements ITaskDramaStepCommand
    {
        private var _callData:TTaskDrama;
        private var _callback:Function;
        private var _lastPoint:Point;
        private var _calling:Boolean = false;

        public function TaskDramaPlayerMoveOnly()
        {
            this._lastPoint = new Point();
            return;
        }// end function

        public function call(param1:TTaskDrama, param2:Function = null) : void
        {
            if (this._calling)
            {
                return;
            }
            this._calling = true;
            this._callData = param1;
            Game.scene.lockSceen();
            this._callback = param2;
            this._lastPoint.x = RolePlayer.instance.x2d;
            this._lastPoint.y = RolePlayer.instance.y2d;
            AIManager.cancelAll();
            AIManager.addMoveTo(new Point(param1.valueOne, param1.valueTwo), 0);
            Dispatcher.addEventListener(EventName.AI_MoveEnd, this.callEndHandler);
            AIManager.start();
            return;
        }// end function

        private function callEndHandler(event:DataEvent) : void
        {
            Dispatcher.removeEventListener(EventName.AI_MoveEnd, this.callEndHandler);
            if (this._callback != null)
            {
                this._callback.apply();
                this._callback = null;
            }
            this._calling = false;
            return;
        }// end function

        public function cancel(param1:TTaskDrama, param2:Function = null) : void
        {
            this._callback = param2;
            AIManager.cancelAll();
            if (param1 != null && (param1.valueOne > 0 || param1.valueTwo > 0))
            {
                AIManager.addMoveTo(new Point(param1.valueOne, param1.valueTwo), 20);
            }
            else if (this._lastPoint.x > 0 || this._lastPoint.y > 0)
            {
                AIManager.addMoveTo(new Point(this._lastPoint.x, this._lastPoint.y), 2);
            }
            Dispatcher.addEventListener(EventName.AI_MoveEnd, this.cancelEndHandler);
            AIManager.start();
            return;
        }// end function

        private function cancelEndHandler(event:DataEvent) : void
        {
            Game.scene.unLockSceen();
            Dispatcher.removeEventListener(EventName.AI_MoveEnd, this.cancelEndHandler);
            if (this._callback != null)
            {
                this._callback.apply();
                this._callback = null;
            }
            return;
        }// end function

        public function dispose() : void
        {
            Game.scene.unLockSceen();
            Dispatcher.removeEventListener(EventName.AI_MoveEnd, this.callEndHandler);
            Dispatcher.removeEventListener(EventName.AI_MoveEnd, this.cancelEndHandler);
            this._callback = null;
            return;
        }// end function

    }
}
